/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess.Utilities
*        文件名:             OleDbHelper.cs
*        创建系统时间:       2012/11/2 19:07:01
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess.Utilities
{
    using System;
    using System.Data;
    using System.Data.Common;
    using System.Data.OleDb;
    
    using ECP.Utilities;

    public class OleDbHelper : BaseDbHelper, IDbHelper
    {
        public OleDbHelper()
        {
            base.FileName = "OleDbHelper.txt";
        }

        public OleDbHelper(string connectionString)
            : this()
        {
            base.dbConnection.ConnectionString = connectionString;
        }

        public override void AttachAdapterParameters(IDataParameter[] commandParameters)
        {
            this.AttachParameters(base.dbDataAdapter.SelectCommand, commandParameters);
        }

        public override void AttachParameters(IDbCommand TargetDbCommand, IDataParameter[] commandParameters)
        {
            ((OleDbCommand)TargetDbCommand).Parameters.AddRange(commandParameters);
        }

        public string GetDBDateTime()
        {
            string sqlQuery = " SELECT " + this.GetDBNow();
            if (BaseConfiguration.Instance.DataBaseType.Equals(DataBaseType.Oracle))
            {
                sqlQuery = sqlQuery + " FROM DUAL ";
            }
            base.Open();
            string str2 = base.ExecuteScalar(CommandType.Text, sqlQuery, null).ToString();
            base.Close();
            return str2;
        }

        public string GetDBNow()
        {
            string str = " Getdate() ";
            switch (BaseConfiguration.Instance.DataBaseType)
            {
                case DataBaseType.Oracle:
                    return " SYSDATE ";

                case DataBaseType.Sqlserver:
                    return " GetDate() ";

                case DataBaseType.Access:
                    return ("'" + DateTime.Now.ToString() + "'");

                case DataBaseType.Bd2:
                    return str;

                case DataBaseType.MySql:
                    return " NOW() ";
            }
            return str;
        }

        public string GetParameter(string parameter)
        {
            return " ? ";
        }

        public DbProviderFactory Instance()
        {
            return OleDbFactory.Instance;
        }

        public DbParameter MakeInParam(string targetFiled, object targetValue)
        {
            OleDbParameter parameter = null;
            if (targetValue != null)
            {
                if (targetValue is string)
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.VarChar);
                }
                else if (targetValue is bool)
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.Boolean);
                }
                else if (targetValue is DateTime)
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.Date);
                }
                else if (targetValue is int)
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.Integer);
                }
                else if (targetValue is long)
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.BigInt);
                }
                else if (targetValue is double)
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.Double);
                }
                else if (targetValue is byte[])
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.Binary);
                }
                else
                {
                    parameter = new OleDbParameter(targetFiled, OleDbType.VarChar);
                }
                parameter.Value = targetValue;
            }
            return parameter;
        }

        public DbParameter MakeInParam(string paramName, DbType DbType, int Size, object Value)
        {
            return this.MakeParam(paramName, DbType, Size, ParameterDirection.Input, Value);
        }

        public DbParameter MakeOutParam(string paramName, DbType DbType, int Size)
        {
            return this.MakeParam(paramName, DbType, Size, ParameterDirection.Output, null);
        }

        public DbParameter MakeParam(string paramName, DbType DbType, int Size, ParameterDirection Direction, object Value)
        {
            OleDbParameter parameter;
            if (Size > 0)
            {
                parameter = new OleDbParameter(paramName, (OleDbType)DbType, Size);
            }
            else
            {
                parameter = new OleDbParameter(paramName, (OleDbType)DbType);
            }
            parameter.Direction = Direction;
            if ((Direction != ParameterDirection.Output) || (Value != null))
            {
                parameter.Value = Value;
            }
            return parameter;
        }

        public DbParameter[] MakeParameters(string targetFiled, object targetValue)
        {
            DbParameter[] parameterArray = null;
            if ((targetFiled != null) && (targetValue != null))
            {
                parameterArray = new DbParameter[] { this.MakeInParam(targetFiled, targetValue) };
            }
            return parameterArray;
        }

        public DbParameter[] MakeParameters(string[] targetFileds, object[] targetValues)
        {
            DbParameter[] parameterArray = new DbParameter[0];
            if ((targetFileds != null) && (targetValues != null))
            {
                parameterArray = new DbParameter[targetFileds.Length];
                for (int i = 0; i < targetFileds.Length; i++)
                {
                    parameterArray[i] = this.MakeInParam(targetFileds[i], targetValues[i]);
                }
            }
            return parameterArray;
        }

        public string PlusSign()
        {
            return " + ";
        }

        public string PlusSign(params string[] values)
        {
            string str = string.Empty;
            switch (BaseConfiguration.Instance.DataBaseType)
            {
                case DataBaseType.Oracle:
                    for (int i = 0; i < values.Length; i++)
                    {
                        str = str + values[i] + " || ";
                    }
                    if (!string.IsNullOrEmpty(str))
                    {
                        return str.Substring(0, str.Length - 4);
                    }
                    return " || ";

                case DataBaseType.Sqlserver:
                case DataBaseType.Access:
                    if (string.IsNullOrEmpty(str))
                    {
                        return " + ";
                    }
                    return str.Substring(0, str.Length - 3);

                case DataBaseType.Bd2:
                    return str;

                case DataBaseType.MySql:
                    str = " CONCAT(";
                    for (int j = 0; j < values.Length; j++)
                    {
                        str = str + values[j] + " ,";
                    }
                    return (str.Substring(0, str.Length - 2) + ")");
            }
            return str;
        }

        public string SqlSafe(string value)
        {
            value = value.Replace("'", "''");
            return value;
        }
    }
}